//
//  progressViewController.m
//  uniform
//
//  Created by Apple on 15/8/30.
//  Copyright (c) 2015年 giuseppe. All rights reserved.
//

#import "progressSearchViewController.h"
#import "progressModel.h"


@interface progressSearchViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    
    UITableView *progressTableView;
    NSMutableArray *progressModelArray;
}

@end

@implementation progressSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *navView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    navView1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView1];
    
    //自定义的导航栏
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    navView.backgroundColor = [UIColor whiteColor];
    navView.alpha = 0.90;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 33, 27, 16);
    [backButton setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, 20, 100, 40)];
    titleLabel.text = self.topicString;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 1)];
    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    
    
    [self initView];
    
    
    [self.view addSubview:view];
    [navView addSubview:backButton];
    [navView addSubview:titleLabel];
    [self.view addSubview:navView];
    
    
}




- (void)loadData{


    //请求参数
    NSDictionary *paras= @{@"userId":@"1",
                           @"suitId":self.suitidString,
                           @"type":self.typeString,
                           @"orderId":self.orderNumberString
                           };

    
    
//    if ([self.typeString isEqualToString:@"5"]) {
//        //请求参数
//        NSDictionary *paras= @{@"userId":@"1",
//                               @"suitId":self.suitidString,
//                               @"type":self.typeString,
//                               @"orderId":self.orderNumberString
//                               };
//        
//    }
//    if ([self.typeString isEqualToString:@"6"]) {
//        
//        //请求参数
//        NSDictionary *paras= @{@"userId":@"1",
//                               @"suitId":self.suitidString,
//                               @"type":self.typeString,
//                               @"orderId":self.orderNumberString
//                               };
//
//        
//    }

    
    
    
    //调用请求
    [[myDataAccess shardWebUtil] orderListsManagementWithURLString:kNetPath_Code_orderRefundProgress   andParas:paras andTransferGottenData:^(id obj, NSError *error)  {
        
        NSLog(@"%@",obj);
        
        if (obj) {
            
            
            
            NSArray *dataList = [obj objectForKey:@"dataList"];
            progressModelArray = [[NSMutableArray alloc]init];
            
            for (NSDictionary *dic in dataList) {
                
                progressModel *model = [progressModel initWithDicAttributes:dic];
  
                [progressModelArray addObject:model];
                    
            }
       
            [progressTableView.legendHeader endRefreshing];
            [progressTableView reloadData];
            
        }else{
            
            //没数据
            
        }
        
        
    }];
    
}



- (void)initView{
    
    progressTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight)];
    progressTableView.delegate = self;
    progressTableView.dataSource = self;
    progressTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    headerView.backgroundColor = [UIColor whiteColor];


    
    UIImageView *goodImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 70, 110)];
    [goodImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrlString]];
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 20, kScreenWidth-90, 30)];
    nameLabel.text = self.nameString;
    
    UILabel *orderNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 50, kScreenWidth-90, 30)];
    orderNumberLabel.text = [NSString stringWithFormat:@"订单号:%@",self.orderNumberString];
    orderNumberLabel.textColor = [UIColor grayColor];
    orderNumberLabel.font = [UIFont systemFontOfSize:15];
    
    UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 80, 60, 30)];
    priceLabel.text = [NSString stringWithFormat:@"￥:%@",self.priceString];
    priceLabel.textColor = mainColor;

    
    if ([self.typeString isEqualToString:@"5"]) {
        self.returnTitleLabel = [[UILabel alloc]init];
        self.returnTitleLabel.adjustsFontSizeToFitWidth = YES;
        self.returnTitleLabel.backgroundColor = [UIColor lightGrayColor];
        self.returnTitleLabel.textColor = [UIColor whiteColor];
        self.returnTitleLabel.layer.masksToBounds = YES;
        self.returnTitleLabel.layer.cornerRadius = 3;
        self.returnTitleLabel.text = @"退货";
        self.returnTitleLabel.textAlignment = NSTextAlignmentCenter;
        self.returnTitleLabel.frame = CGRectMake(150, 80, 40, 20);
        [headerView addSubview:self.returnTitleLabel];
    }
    if ([self.typeString isEqualToString:@"6"]) {
        
        self.exchangeTitleLabel = [[UILabel alloc]init];
        self.exchangeTitleLabel.adjustsFontSizeToFitWidth = YES;
        self.exchangeTitleLabel.backgroundColor = mainColor;
        self.exchangeTitleLabel.textColor = [UIColor whiteColor];
        self.exchangeTitleLabel.layer.masksToBounds = YES;
        self.exchangeTitleLabel.layer.cornerRadius = 3;
        self.exchangeTitleLabel.text = @"换货";
        self.exchangeTitleLabel.textAlignment = NSTextAlignmentCenter;
        self.exchangeTitleLabel.frame = CGRectMake(150, 80, 40, 20);
        [headerView addSubview:self.exchangeTitleLabel];
    }
    

    [headerView addSubview:goodImageView];
    [headerView addSubview:nameLabel];
    [headerView addSubview:orderNumberLabel];
    [headerView addSubview:priceLabel];
    
    
    
    progressTableView.tableHeaderView = headerView;
    
    [self.view addSubview:progressTableView];


    __weak __typeof(self) weakSelf = self;
    
    [progressTableView  addLegendHeaderWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [progressTableView.legendHeader beginRefreshing];
    

}



#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return progressModelArray.count;
    
}

#pragma mark--- tableViewDelegate 区尾的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;
    
}


#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *indentify = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentify];

    //自定义分割线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, kScreenWidth-50, 1)];
    lineView.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    
    [cell.contentView addSubview:lineView];

    
    if (indexPath.row == 0) {
        
        lineView.frame = CGRectMake(0, 0, kScreenWidth, 1);
    }
    
    
    //添加图片
    UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 10, progressTableView.rowHeight)];
    cellImageView.image = [UIImage imageNamed:@"progress_during"];
    if ([self.topicString isEqualToString:@"查看物流"]) {
        if (indexPath.row == 0) {
            cellImageView.frame = CGRectMake(10, 20, 10, progressTableView.rowHeight-20);
            cellImageView.image = [UIImage imageNamed:@"progress_top1"];
        }
    }else{
        if (indexPath.row == 0) {
            cellImageView.frame = CGRectMake(10, 20, 10, progressTableView.rowHeight-20);
            cellImageView.image = [UIImage imageNamed:@"progress_top2"];
        }
    }

    if (indexPath.row == progressModelArray.count-1) {
        cellImageView.frame = CGRectMake(10, 0, 10, progressTableView.rowHeight-20);
        cellImageView.image = [UIImage imageNamed:@"progress_end"];
    }
    
    
    
    [cell.contentView addSubview:cellImageView];
    
    
    progressModel *tmp = progressModelArray[indexPath.row];
 
    UILabel *contentsLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 20, kScreenWidth-50, 30)];
    contentsLabel.text = tmp.brief;
    contentsLabel.textColor = [UIColor grayColor];

    UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, kScreenWidth-50, 20)];
    dateLabel.text = tmp.timeStamp;
    dateLabel.textColor = [UIColor grayColor];
    
    
    if (indexPath.row == 0) {
        contentsLabel.textColor = mainColor;
    }
    
    [cell.contentView addSubview:contentsLabel];
    [cell.contentView addSubview:dateLabel];
    

    
    return cell;
    
    
}

#pragma mark--- tableViewDelegate 点击方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


- (void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
